<template>
  <el-dialog :visible="showRoleDialog" append-to-body @close="hClose">

    <!-- // 分配角色 -->
    <div>
      <el-checkbox-group v-model="roleIds">
        <!-- 注意：label决定当前选中的值 -->
        <el-checkbox v-for="item in roleList" :key="item.id" :label="item.id">
          {{ item.name }}
        </el-checkbox>
      </el-checkbox-group>

      <div style="margin-top: 20px; text-align: right">
        <el-button type="primary" @click="primary">确定</el-button>
        <el-button @click="hClose">取消</el-button>
      </div>
    </div>
  </el-dialog>
</template>
<script>
import { getEnableRoleList, getEmployeeDetail } from '@/api/employee'
import { assignRole } from '@/api/role'
export default {
  props: {
    showRoleDialog: {
      type: Boolean,
      default: false
    },
    curId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      roleIds: [],
      roleList: []
    }
  },
  created() {
    this.loadRoles()
  },
  methods: {
    hClose() {
      this.$emit('update:show-role-dialog', false)
    },
    async loadRoles() {
      const res = await getEnableRoleList()
      this.roleList = res.data
    },
    async getRoseById(id) {
      const res = await getEmployeeDetail(this.curId)
      // 数据回显
      this.roleIds = res.data.roleIds
    },
    async primary() {
      console.log(this.curId, this.roleIds)
      const res = await assignRole({ id: this.curId, roleIds: this.roleIds })
      console.log(res)
      this.$message.success('分配角色成功')
      this.hClose()
    }
  }
}
</script>
